<!-- 指令用量录入：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">汇总指令</h1>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" @currentRow="currentRow" />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">指令材料</h1>
        <Table
          :tableHeader="tableHeader2"
          :tableData="tableData2"
          class="mt10"
          :isLoading="isLoading2"
          @currentRow="currentRow2"
          @inputChange="inputChange2"
          :TableHeight="50"
          :outTableHeight="100"
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total: total2, buttonS: true }" @pageSizeChange="handleSizeChange2" @pageChange="handleCurrentChange2" />
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
          <span style="font-weight: 600">贴合子资料查询</span>
          <Table
            :TableHeight="50"
            :outTableHeight="100"
            @inputChange="inputChange5"
            :isLoading="isLoading5"
            :tableHeader="tableHeader5"
            :tableData="tableData5"
          ></Table>
        </el-card>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup name="InstructionInput">
import JMessage from '@/utils/JMessage'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { watch } from 'vue'
import { onMounted } from 'vue'
import { ref } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '检核指令单'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '取消原因',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '取消检核'
  },
  {
    type: 'button',
    checkboxLabel: 'BOM材料变更'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '检核指令单') {
    const res = await service.get<any, any>(`/mes/kf/lotUsageInput/checkSummaryLotNo?lotNo=${currentTableRow.value.lotNo}`)
    if (res.code === 200) {
      JMessage.success(res.msg)
      getTableList()
    } else {
      JMessage.error(res.msg)
    }
  }
  if (content === '取消检核') {
    // if (!formList.value.find((item: any) => item.selectP === '取消原因')?.selectValue) {
    //   JMessage.warning('请选择取消原因')
    //   return
    // }
    const res = await service.get<any, any>(
      `/mes/kf/lotUsageInput/cancelCheckSummaryLotNo?lotNo=${currentTableRow.value.lotNo}&remark=${
        formList.value.find((item: any) => item.selectP === '取消原因')?.selectValue
      }`
    )
    if (res.code === 200) {
      JMessage.success(res.msg)
      getTableList()
    } else {
      JMessage.error(res.msg)
    }
  }
  if (content === 'BOM材料变更') {
    let res = await service.get<any, any>(`/mes/kf/lotUsageInput/bomChange?lotNo=${currentTableRow.value.lotNo}`)
    if (res.code === 200) {
      JMessage.success(res.msg)
      getTableList2()
    } else {
      JMessage.error(res.msg)
    }
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const tableHeader = ref([
  {
    backgroundColor: true,
    label: '指令',
    prop: 'lotNo',
    width: 120
  },
  {
    label: '汇总号',
    prop: 'sumNo',
    width: 120
  },

  {
    label: '工厂货号',
    prop: 'style'
  },
  {
    label: '颜色',
    prop: 'color'
  },
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '纸板号',
    prop: 'shoeName'
  },
  {
    label: '号码',
    prop: 'sizeNo'
  },
  // {
  //   label: '下单日期',
  //   prop: 'orderDate'
  // },
  {
    label: '下单数量',
    prop: 'orderQty'
  },
  // {
  //   label: '样品类型',
  //   prop: 'sampleType'
  // },
  {
    label: '检核时间',
    prop: 'cfmTime'
  }
])
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/mes/kf/lotUsageInput/getSummaryLotNoList', {
    lotNo: formList.value.find((item: any) => item.inputPlaceholder === '请输入指令')?.inputValue,
    // mtlName: formList.value.find((item: any) => item.inputPlaceholder === '请输入材料名称')?.inputValue,
    // mtlNo: formList.value.find((item: any) => item.inputPlaceholder === '请输入材料编码')?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    deptType: 2
  })
  isLoading.value = false
  tableData.value = tableDataList.rows.map((item: any) => {
    if (item.cfmFlag === 'Y') item.backgroundColor = '#14B815'
    return item
  })
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0]) => {
  currentTableRow.value = row
  getTableList2()
}

// 表二模块
const tableData2 = ref([])
const isLoading2 = ref(true)
const tableHeader2 = ref([
  {
    change: true,
    label: '物料编码',
    prop: 'mtlNo',
    width: 120
  },
  {
    change: true,
    label: '材料颜色',
    prop: 'mtlColor',
    width: 120
  },
  {
    change: true,
    label: '材料名称',
    prop: 'mtlName',
    width: 150
  },
  {
    change: true,
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    change: true,
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    change: true,
    label: '预加量',
    prop: 'advanceUsage'
  },
  {
    label: '用量',
    prop: 'ttlUsage'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
const total2 = ref(0)
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/mes/kf/lotUsageInput/getPc042KfTempByLotNo', {
    ...currentTableRow.value,
    deptType: 2
  })
  isLoading2.value = false
  tableData2.value = tableDataList.rows.map((item: any) => {
    return {
      ...item,
      change: 'Y',
      disableConfirm: true
    }
  })
  total2.value = tableDataList.total
  currentRow2()
}, 500)
const currentTableRow2 = ref({}) as any
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
const currentRow2 = (row: any = tableData2.value[0]) => {
  currentTableRow2.value = row
  getTableList5()
}

// 修改后点击确定按钮
const inputChange2 = async (row: any) => {
  let res = await service.post<any, any>(`/mes/kf/lotUsageInput/updatePc042KfTemp`, row)
  if (res.code === 200) {
    JMessage.success(res.msg)
  } else {
    JMessage.error(res.msg)
    getTableList2()
  }
}

// 贴合子资料查询
const isLoading5 = ref(true)
const currentPage5 = ref(1)
const pageSize5 = ref(40)
const handleSizeChange5 = (val: number) => {
  pageSize5.value = val
  getTableList5()
}
const handleCurrentChange5 = (val: number) => {
  currentPage5.value = val
  getTableList5()
}
const tableData5 = ref([])
const total5 = ref(0)
const getTableList5 = async () => {
  isLoading5.value = true
  const result = await service.post<any, any>(`/mes/kf/lotUsageInput/getBondingSubmaterial`, {
    ...currentTableRow.value,
    ...currentTableRow2.value
  })
  tableData5.value = result.rows.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })
  total5.value = result.total
  isLoading5.value = false
}
const inputChange5 = async (row: any) => {
  let res = await service.post<any, any>(`/mes/kf/lotUsageInput/updatePc044Kf`, row)
  if (res.code === 200) {
    JMessage.success('修改成功')
  } else {
    JMessage.error(res.msg)
    getTableList5()
  }
}

const tableHeader5 = ref([
  {
    change: true,
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    change: true,
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    change: true,
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    change: true,
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    change: true,
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '用量',
    prop: 'ttlUsage'
  },
  {
    change: true,
    label: '备注',
    prop: 'remark'
  }
])

const getSelectList = async () => {
  const res = await service.get<any, any>(`/mes/kf/lotUsageInput/getReason?reason=${''}`)
  if (res.code === 200) {
    formList.value.find((item: any) => item.selectP === '取消原因')!.selectOptions = res.rows
  }
}
onMounted(() => {
  getSelectList()
})
</script>
